(function(){BX.namespace("BX.rest");if(!!BX.rest.AppLayout){return}BX.rest.AppLayout=function(e){this.params={firstRun:!!e.firstRun,appHost:e.appHost,appPort:e.appPort,appProto:e.appProto,authId:e.authId,authExpires:e.authExpires,refreshId:e.refreshId,placement:e.placement,formName:e.formName,frameName:e.frameName,loaderName:e.loaderName,layoutName:e.layoutName,ajaxUrl:e.ajaxUrl,controlUrl:e.controlUrl,isAdmin:!!e.isAdmin,staticHtml:!!e.staticHtml,id:e.id,appId:e.appId,appV:e.appV,appI:e.appI,appSid:e.appSid,memberId:e.memberId,restPath:e.restPath,proto:e.proto,userOptions:e.userOptions,appOptions:e.appOptions,placementId:!!e.placementId?e.placementId:0,placementOptions:e.placementOptions};this.userSelectorControl=[null,null];this.userSelectorControlCallback=null;this.bAccessLoaded=false;this._appOptionsStack=[];this._inited=false;this._destroyed=false;this.deniedInterface=[];this.selectUserCallback_1_value=[];this.messageInterface=new(BX.rest.AppLayout.initializePlacement(this.params.placement));BX.bind(window,"message",BX.proxy(this.receiveMessage,this))};BX.rest.AppLayout.openApplication=function(e,t,a,s){var r=BX.message("REST_APPLICATION_URL").replace("#ID#",parseInt(e));r=BX.util.add_url_param(r,{_r:Math.random()});var n={};if(t&&typeof t==="object"){for(var i in t){if(!t.hasOwnProperty(i)){continue}if(i.search("bx24_")===0){var o=i.replace("bx24_","");n[o]=t[i];delete t[i]}}if(t.hasOwnProperty("options")){if(typeof t.options==="object"){appOptions=t.options}if(t.hasOwnProperty("params")){t=t.params}}}var p={ID:e,PLACEMENT_OPTIONS:t,POPUP:1};if(!!a){if(typeof a.PLACEMENT!=="undefined"){p.PLACEMENT=a.PLACEMENT}if(typeof a.PLACEMENT_ID!=="undefined"){p.PLACEMENT_ID=a.PLACEMENT_ID}}var l={url:r,anchor:null,target:null};var c=BX.SidePanel.Instance.getUrlRule(r,l);var u=c&&c.options?BX.clone(c.options):{};u["cacheable"]=false;u["contentCallback"]=function(e){var t=new top.BX.Promise;top.BX.ajax.post(e.url,{sessid:BX.bitrix_sessid(),site:BX.message("SITE_ID"),PARAMS:{template:"",params:p}},(function(e){t.fulfill(e)}));return t};u["events"]=u["events"]?u["events"]:{};u["events"]["onClose"]=function(){if(!!s){s()}};var f=["width","leftBoundary","title","label"];for(var h in n){if(!n.hasOwnProperty(h)){continue}for(var d in f){if(h===f[d]){switch(h){case"leftBoundary":if(BX.type.isNumber(n[h])){u["customLeftBoundary"]=Number(n[h])}break;case"width":if(BX.type.isNumber(n[h])){u["width"]=Number(n[h])}break;case"title":if(BX.type.isString(n[h])){u["title"]=String(n[h])}break;case"label":var m=n[h];if(BX.type.isObject(m)){var B={aqua:"#06bab1",green:"#a5de00",orange:"#ffa801",brown:"#b57051",pink:"#f968b6",blue:"#2eceff",grey:"#a1a6ac",violet:"#6b52cc"};if(m.hasOwnProperty("bgColor")){var X="";for(var I in B){if(m.bgColor===I){X=B[I];break}}n[h]["bgColor"]=X}u["label"]=n[h]}break}}}}BX.SidePanel.Instance.open(r,u);var y=top.BX.SidePanel.Instance.getTopSlider();top.BX.addCustomEvent(top,"Rest:AppLayout:ApplicationInstall",(function(r,n){n.redirect=false;y.close(false,(function(){BX.rest.AppLayout.openApplication(e,t,a,s)}))}))};BX.rest.AppLayout.openPath=function(e,t,a){var s=BX.type.isString(t["path"])?t["path"]:"";var r=/^\/(crm\/(deal|lead|contact|company|type)|marketplace|company\/personal\/user\/[0-9]+|workgroups\/group\/[0-9]+)\//;if(!BX.browser.IsMobile()){if(s!==""&&r.test(s)){var n="from=rest_placement&from_app="+e;s+=(s.indexOf("?")===-1?"?":"&")+n;var i={url:s,anchor:null,target:null};var o=BX.SidePanel.Instance.getUrlRule(s,i);var p=o&&o.options?BX.clone(o.options):{};p["cacheable"]=false;if(!("events"in p)){p["events"]={}}p["events"]["onClose"]=function(){if(!!a&&BX.type.isFunction(a)){a({result:"close"})}};BX.SidePanel.Instance.open(s,p)}else{if(!!a&&BX.type.isFunction(a)){a({result:"error",errorCode:"PATH_NOT_AVAILABLE"})}}}else{a({result:"error",errorCode:"METHOD_NOT_SUPPORTED_ON_DEVICE"})}};BX.rest.AppLayout.prototype={init:function(){if(!this._inited&&!!document.forms[this.params.formName]){var e=BX(this.params.loaderName);BX.bind(BX(this.params.frameName),"load",(function(){BX.addClass(e,"app-loading-msg-loaded");BX.removeClass(this,"app-loading");BX.remove(e)}));if(this.params.staticHtml){BX(this.params.frameName).src=document.forms[this.params.formName].action}else{document.forms[this.params.formName].submit()}this._inited=true}},destroy:function(){BX.unbind(window,"message",BX.proxy(this.receiveMessage,this));if(BX(this.params.frameName)){BX(this.params.frameName).parentNode.removeChild(BX(this.params.frameName))}this._destroyed=true},query:function(e,t){var a={sessid:BX.bitrix_sessid(),site:BX.message("SITE_ID"),PARAMS:{template:"",params:{ID:this.params.id}}};if(!!e){a=BX.mergeEx(a,e)}return BX.ajax({dataType:"json",method:"POST",url:this.params.ajaxUrl,data:a,onsuccess:t})},receiveMessage:function(t){t=t||window.event;if(t.origin!==this.params.appProto+"://"+this.params.appHost&&t.origin+":"+this.params.appPort!==this.params.appProto+"://"+this.params.appHost||!BX.type.isString(t.data)&&!BX.type.isObject(t.data)){return}var a={},s=[],r="",n="",i=false;if(BX.type.isObject(t.data)){n=t.data.method;r=t.data.appSid;i=t.data.callback;s=!!t.data.params?t.data.params:[]}else{a=e(t.data,":");n=a[0];i=a[2];r=a[3];if(a[1]){s=JSON.parse(a[1])}}if(r!=this.params.appSid){return}if(!!this.messageInterface[n]&&!BX.util.in_array(n,this.deniedInterface)){var o=!i?BX.DoNothing:BX.delegate((function(e){var t=BX(this.params.frameName);if(!!t&&!!t.contentWindow){t.contentWindow.postMessage(i+":"+(typeof e=="undefined"?"":JSON.stringify(e)),this.params.appProto+"://"+this.params.appHost)}}),this);this.messageInterface[n].apply(this,[s,o,this])}},denyInterface:function(e){this.deniedInterface=BX.util.array_merge(this.deniedInterface,e)},allowInterface:function(e){var t=[];for(var a=0;a<this.deniedInterface.length;a++){if(!BX.util.in_array(this.deniedInterface[a],e)){t.push(this.deniedInterface[a])}}this.deniedInterface=t},sendAppOptions:function(){if(this._appOptionsStack.length>0){var e=this._appOptionsStack;this._appOptionsStack=[];var t=[];for(var a=0;a<e.length;a++){t.push({name:e[a][0],value:e[a][1]})}var s={action:"set_option",options:t};this.query(s,(function(t){for(var a=0;a<e.length;a++){e[a][2](t)}}))}},loadControl:function(e,t,a){if(!t){t={}}t.control=e;t.sessid=BX.bitrix_sessid();BX.ajax({method:"POST",url:this.params.controlUrl,data:t,processScriptsConsecutive:true,onsuccess:a})},reInstall:function(){BX.proxy(this.messageInterface.setInstallFinish,this)({value:false})},selectUserCallback_0:function(e){var t=BX.util.array_values(e);if(!!t&&t.length>0){BX.defer(this.userSelectorControl[0].close,this.userSelectorControl[0])();if(!!this.userSelectorControlCallback){this.userSelectorControlCallback.apply(this,[t[0]])}}},selectUserCallback_1:function(e){if(e===true){var t=BX.util.array_values(this.selectUserCallback_1_value);BX.defer(this.userSelectorControl[1].close,this.userSelectorControl[1])();if(!!this.userSelectorControlCallback){this.userSelectorControlCallback.apply(this,[t])}}else{this.selectUserCallback_1_value=e}},hideUpdate:function(e,t){BX.userOptions.save("app_options","params_"+this.params.appId+"_"+this.params.appV,"skip_update_"+e,1);t()}};BX.rest.AppLayout.initizalizePlacementInterface=function(e){var t=function(){};BX.extend(t,e);t.prototype.events=BX.clone(t.superclass.events);return t};BX.rest.AppLayout.initializePlacement=function(e){e=(e+"").toUpperCase();if(!BX.rest.AppLayout.placementInterface[e]){BX.rest.AppLayout.placementInterface[e]=BX.rest.AppLayout.initizalizePlacementInterface(e==="DEFAULT"?BX.rest.AppLayout.MessageInterface:BX.rest.AppLayout.MessageInterfacePlacement)}return BX.rest.AppLayout.placementInterface[e]};BX.rest.AppLayout.initializePlacementByEvent=function(e,t){BX.addCustomEvent(t,(function(t){var a=BX.rest.AppLayout.initializePlacement(e);if(!!t.events){for(var s=0;s<t.events.length;s++){a.prototype.events.push(t.events[s])}}for(var r in t){if(r!=="events"&&t.hasOwnProperty(r)){a.prototype[r]=t[r]}}}))};BX.rest.AppLayout.MessageInterface=function(){};BX.rest.AppLayout.MessageInterface.prototype={events:[],getInitData:function(e,t){t({LANG:BX.message("LANGUAGE_ID"),DOMAIN:location.host,PROTOCOL:this.params.proto,PATH:this.params.restPath,AUTH_ID:this.params.authId,AUTH_EXPIRES:this.params.authExpires,REFRESH_ID:this.params.refreshId,MEMBER_ID:this.params.memberId,FIRST_RUN:this.params.firstRun,IS_ADMIN:this.params.isAdmin,INSTALL:this.params.appI,USER_OPTIONS:this.params.userOptions,APP_OPTIONS:this.params.appOptions,PLACEMENT:this.params.placement,PLACEMENT_OPTIONS:this.params.placementOptions});this.params.firstRun=false},getInterface:function(e,t){var a={command:[],event:[]};for(var s in this.messageInterface){if(s!=="events"&&s!=="constructor"&&!BX.rest.AppLayout.MessageInterfacePlacement.prototype[s]&&!BX.util.in_array(s,this.deniedInterface)){a.command.push(s)}}for(var r=0;r<this.messageInterface.events.length;r++){a.event.push(this.messageInterface.events[r])}t(a)},refreshAuth:function(e,t){e={action:"access_refresh"};this.query(e,BX.delegate((function(e){if(!!e["access_token"]){this.params.authId=e["access_token"];this.params.authExpires=e["expires_in"];this.params.refreshId=e["refresh_token"];t({AUTH_ID:this.params.authId,AUTH_EXPIRES:this.params.authExpires,REFRESH_ID:this.params.refreshId})}else{alert("Unable to get new token! Reload page, please!")}}),this))},resizeWindow:function(e,t){var a=BX(this.params.layoutName);e.width=e.width=="100%"?e.width:(parseInt(e.width)||100)+"px";e.height=parseInt(e.height);if(!!e.width){a.style.width=e.width}if(!!e.height){a.style.height=e.height+"px"}var s=BX.pos(a);t({width:s.width,height:s.height})},setTitle:function(e,t){BX.ajax.UpdatePageTitle(e.title);t(e)},setScroll:function(e,t){if(!!e&&typeof e.scroll!="undefined"&&e.scroll>=0){window.scrollTo(BX.GetWindowScrollPos().scrollLeft,parseInt(e.scroll))}t(e)},setUserOption:function(e,t){this.params.userOptions[e.name]=e.value;BX.userOptions.save("app_options","options_"+this.params.appId,e.name,e.value);t(e)},setAppOption:function(e,t){if(this.params.isAdmin){this._appOptionsStack.push([e.name,e.value,t]);BX.defer(this.sendAppOptions,this)()}},setInstall:function(e,t){BX.userOptions.save("app_options","params_"+this.params.appId+"_"+this.params.appV,"install",!!e["install"]?1:0);t(e)},setInstallFinish:function(e,t){var a={action:"set_installed",v:typeof e.value=="undefined"||e.value!==false?"Y":"N"};this.query(a,BX.delegate((function(e){var t={redirect:true};top.BX.onCustomEvent(top,"Rest:AppLayout:ApplicationInstall",[a.v,t],false);if(t.redirect){window.location=BX.util.add_url_param(window.location.href,{install_finished:!!e.result?"Y":"N"})}}),this))},selectUser:function(e,t){this.userSelectorControlCallback=t;var a=parseInt(e.mult+0);if(a){if(this.userSelectorControl[a]){this.userSelectorControl[a].close();this.userSelectorControl[a].destroy();this.userSelectorControl[a]=null}}else if(!!this.userSelectorControl[a]){this.userSelectorControl[a].show();return}var s={name:"USER_"+a,onchange:"user_selector_cb_"+parseInt(Math.random()*1e5),site_id:BX.message("SITE_ID")};if(a){s.mult=true}window[s.onchange]=BX.delegate(this["selectUserCallback_"+a],this);this.loadControl("user_selector",s,BX.delegate((function(t){this.userSelectorControl[a]=BX.PopupWindowManager.create("app-user-popup-"+a,null,{autoHide:true,content:t,zIndex:5e3});if(a){this.userSelectorControl[a].setButtons([new BX.PopupWindowButton({text:BX.message("REST_ALT_USER_SELECT"),className:"popup-window-button-accept",events:{click:function(){window[s.onchange](true)}}})])}this.userSelectorControl[parseInt(e.mult+0)].show();BX("USER_"+a+"_selector_content").style.display="block"}),this))},selectAccess:function(e,t){if(!this.bAccessLoaded){this.loadControl("access_selector",{},BX.defer((function(){this.bAccessLoaded=true;BX.defer(this.messageInterface.selectAccess,this)(e,t)}),this))}else{BX.Access.Init({groups:{disabled:true}});e.value=e.value||[];var a={};for(var s=0;s<e.value.length;s++){a[e.value[s]]=true}BX.Access.SetSelected(a);BX.Access.ShowForm({zIndex:5e3,callback:function(e){var a=[];for(var s in e){if(e.hasOwnProperty(s)){for(var r in e[s]){if(e[s].hasOwnProperty(r)){a.push(e[s][r])}}}}t(a)}})}},selectCRM:function(e,t,a){if(a!==true){this.loadControl("crm_selector",{entityType:e.entityType,multiple:!!e.multiple?"Y":"N",value:e.value},BX.delegate((function(){BX.defer(this.messageInterface.selectCRM,this)(e,t,true)}),this));return}if(!window.obCrm){setTimeout(BX.delegate((function(){BX.proxy(this.messageInterface.selectCRM,this)(e,t,true)}),this),500)}else{obCrm["restCrmSelector"].Open();obCrm["restCrmSelector"].AddOnSaveListener((function(e){t(e);obCrm["restCrmSelector"].Clear()}))}},reloadWindow:function(){window.location.reload()},imCallTo:function(e){top.BXIM.callTo(e.userId,!!e.video)},imPhoneTo:function(e){top.BXIM.phoneTo(e.phone)},imOpenMessenger:function(e){top.BXIM.openMessenger(e.dialogId)},imOpenHistory:function(e){top.BXIM.openHistory(e.dialogId)},openApplication:function(e,t){BX.rest.AppLayout.openApplication(this.params.id,e,{},t)},openPath:function(e,t){BX.rest.AppLayout.openPath(this.params.appId,e,t)},closeApplication:function(e,t){var a=BX.message("REST_APPLICATION_VIEW_URL").replace("#APP#",this.params.appId);if(top.BX.SidePanel.Instance.isOpen()&&top.BX.SidePanel.Instance.getTopSlider().url.match(new RegExp("^"+a))){top.BX.SidePanel.Instance.close(false,t)}else{a=BX.message("REST_PLACEMENT_URL").replace("#PLACEMENT_ID#",parseInt(this.params.placementId));if(top.BX.SidePanel.Instance.isOpen()&&top.BX.SidePanel.Instance.getTopSlider().url.match(new RegExp("^"+a))){top.BX.SidePanel.Instance.close(false,t)}else{a=BX.message("REST_APPLICATION_URL").replace("#ID#",parseInt(this.params.id));if(top.BX.SidePanel.Instance.isOpen()&&top.BX.SidePanel.Instance.getTopSlider().url.match(new RegExp("^"+a))){top.BX.SidePanel.Instance.close(false,t)}}}}};BX.rest.AppLayout.MessageInterfacePlacement=BX.rest.AppLayout.initizalizePlacementInterface(BX.rest.AppLayout.MessageInterface);BX.rest.AppLayout.MessageInterfacePlacement.prototype.placementBindEvent=function(e,t){if(!!e.event&&BX.util.in_array(e.event,this.messageInterface.events)){var a=BX.delegate((function(){if(!this._destroyed){t.apply(this,arguments)}else{BX.removeCustomEvent(e.event,a)}}),this);BX.addCustomEvent(e.event,a)}};BX.rest.layoutList={};BX.rest.placementList={};BX.rest.AppLayout.placementInterface={};BX.rest.AppLayout.get=function(e){return BX.rest.layoutList[e]};BX.rest.AppLayout.set=function(e,t,a){e=(e+"").toUpperCase();a.appSid=t;a.placement=e;BX.rest.layoutList[t]=new BX.rest.AppLayout(a);return BX.rest.layoutList[t]};BX.rest.AppLayout.getPlacement=function(e){return BX.rest.placementList[(e+"").toUpperCase()]};BX.rest.AppLayout.setPlacement=function(e,t){BX.rest.placementList[(e+"").toUpperCase()]=t};BX.rest.AppLayout.initialize=function(e,t){e=(e+"").toUpperCase();BX.rest.layoutList[e]=BX.rest.layoutList[t];BX.rest.layoutList[e].init()};BX.rest.AppLayout.destroy=function(e){var t=BX.rest.AppLayout.get(e);if(!!t){t.destroy()}BX.rest.layoutList[t.params.appSid]=null;if(!!BX.rest.AppLayout.placementInterface[e]){BX.rest.layoutList[e]=null}};function e(e,t){var a=e.split(t);return[a[0],a.slice(1,a.length-2).join(t),a[a.length-2],a[a.length-1]]}})();
//# sourceMappingURL=applayout.map.js